//
//  AccountCenterFooterView.m
//  JiaoX
//
//  Created by jiaoxiu－005 on 15/1/16.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "AccountCenterFooterView.h"

#define titleLabelColor JXColor(77, 77, 77)
#define ACCOUNT_CENTER_FOOTER_VIEW_FREIGHT [DimensUtils getScaleX:13]
#define ACCOUNT_CENTER_FOOTER_VIEW_TITLE [DimensUtils getScaleX:13]
@interface AccountCenterFooterView()<AccountCenterPayModeViewDelegate>
{
    AddressAndCheckProdInfo *_localInfo;
}
@property(nonatomic,strong)UIView * freightlineView;
@property(nonatomic,strong)UILabel *freightLeftLabel;
@property(nonatomic,strong)UILabel *freightRightLabel;
@property(nonatomic,strong)UIImageView *payModeTitleIconView;
@property(nonatomic,strong)UILabel *payModeTitleLabel;
@property(nonatomic,strong)UIView * payModelineView;
@property(nonatomic,strong)AccountCenterPayModeView *cargoPay;
@property(nonatomic,strong)AccountCenterPayModeView *aliPay;
@property(nonatomic,strong)AccountCenterPayModeView *wxPay;
@property(nonatomic,strong)UIView *wxPaylineView;
@property(nonatomic,strong)UIImageView *feedbackTitleIconView;
@property(nonatomic,strong)UIImageView *volumeTitleIconView;
@property(nonatomic,strong)UILabel *feedbackTitleLabel;
@property(nonatomic,strong)UILabel *volumeTitleLabel;
@property(nonatomic,strong)UIView *feedbackLineView;
@property(nonatomic,strong)UIView *volumeLineView;
@property(nonatomic,strong)UITextField *feedbackInput;
@property(nonatomic,strong)VolumeFooterView *volumeView;

@end

@implementation AccountCenterFooterView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //0.分割线
        UIView * freightlineView = [[UIView alloc]initWithFrame:CGRectMake(JXPublicInset, 0, JXScreenW-JXPublicInset, 0.5)];
        [freightlineView setBackgroundColor:JXColor(204, 204, 204)];
        _freightlineView = freightlineView;
        [self addSubview:freightlineView];
        
        //1.运费UILabel
        UILabel *freightLeftLabel = [[UILabel alloc]init];
        NSString *freightLeftLabelText = @"运费";
        [freightLeftLabel setText:freightLeftLabelText];
        [freightLeftLabel setFont:[UIFont systemFontOfSize:ACCOUNT_CENTER_FOOTER_VIEW_FREIGHT]];
        [freightLeftLabel setTextColor:JXColor(102, 102, 102)];
        CGSize freightLeftLabelTextSize = [freightLeftLabelText sizeWithFont:[UIFont systemFontOfSize:ACCOUNT_CENTER_FOOTER_VIEW_FREIGHT]];
        [freightLeftLabel setFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(self.freightlineView.frame)+JXPublicInset, freightLeftLabelTextSize.width, freightLeftLabelTextSize.height)];
        _freightLeftLabel = freightLeftLabel;
        [self addSubview:freightLeftLabel];
        
        //2.包邮UILabel
        UILabel *freightRightLabel = [[UILabel alloc]init];
        [freightRightLabel setTextAlignment:NSTextAlignmentRight];
        NSString *freightRightLabelText = @"包邮";
        [freightRightLabel setText:freightRightLabelText];
        [freightRightLabel setFont:[UIFont systemFontOfSize:ACCOUNT_CENTER_FOOTER_VIEW_FREIGHT]];
        [freightRightLabel setTextColor:JXColor(0, 0, 0)];
        CGSize freightRightTextSize = [freightRightLabelText sizeWithFont:[UIFont systemFontOfSize:ACCOUNT_CENTER_FOOTER_VIEW_FREIGHT]];
        [freightRightLabel setFrame:CGRectMake(JXScreenW-50-JXPublicInset, CGRectGetMaxY(self.freightlineView.frame)+JXPublicInset, 50, freightRightTextSize.height)];
        _freightRightLabel = freightRightLabel;
        [self addSubview:freightRightLabel];
        
        //3.支付方式UIImageView
        UIImageView *payModeTitleIconView =[[UIImageView alloc]init];
        UIImage *payModeTileIcon = [UIImage imageNamed:@"account_center_pay_mode_icon"];
        [payModeTitleIconView setImage:payModeTileIcon];
        [payModeTitleIconView setFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(self.freightLeftLabel.frame)+32, payModeTileIcon.size.width, payModeTileIcon.size.height)];
        _payModeTitleIconView = payModeTitleIconView;
        [self addSubview:payModeTitleIconView];
        
        //4.支付方式UILabel
        NSString *payModeTitleText = @"支付方式";
        UILabel *payModeTitleLabel = [[UILabel alloc]init];
        [payModeTitleLabel setText:payModeTitleText];
        [payModeTitleLabel setTextColor:titleLabelColor];
        [payModeTitleLabel setFont:[UIFont systemFontOfSize:ACCOUNT_CENTER_FOOTER_VIEW_TITLE]];
        CGSize payModeTileSize = [payModeTitleText sizeWithFont:[UIFont systemFontOfSize:ACCOUNT_CENTER_FOOTER_VIEW_TITLE]];
        [payModeTitleLabel setFrame:CGRectMake(CGRectGetMaxX(self.payModeTitleIconView.frame)+5, CGRectGetMaxY(self.freightLeftLabel.frame)+30, payModeTileSize.width, payModeTileSize.height)];
        _payModeTitleLabel = payModeTitleLabel;
        [self addSubview:payModeTitleLabel];
        
        //5.分割线UIView
        UIView * payModelineView = [[UIView alloc]initWithFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(self.payModeTitleIconView.frame)+JXPublicInset, JXScreenW-JXPublicInset, 0.5)];
        [payModelineView setBackgroundColor:JXColor(204, 204, 204)];
        _payModelineView = payModelineView;
        [self addSubview:payModelineView];
        
        //6.货到付款
        AccountCenterPayModeView *cargoPay = [[AccountCenterPayModeView alloc]init];
        [cargoPay setIcon:@"account_center_cargopay_icon"];
        [cargoPay setDesc:@"货到付款"];
        [cargoPay setCondition:@"满169元包邮"];
        [cargoPay setIsChecked:NO];
        [cargoPay setIsHidenLine:NO];
        cargoPay.x = 0;
        cargoPay.y = CGRectGetMaxY(self.payModelineView.frame);
        _cargoPay = cargoPay;
        [cargoPay setTag:PAYMODE_CARGO_PAY];
        cargoPay.delegate = self;
        [self addSubview:cargoPay];
        
        //7.支付宝
        AccountCenterPayModeView *aliPay = [[AccountCenterPayModeView alloc]init];
        [aliPay setIcon:@"account_center_alipay_icon"];
        [aliPay setDesc:@"支付宝"];
        [aliPay setCondition:@"满69元包邮"];
        [aliPay setIsChecked:NO];
        [aliPay setIsHidenLine:NO];
        aliPay.x = 0;
        aliPay.y = CGRectGetMaxY(self.cargoPay.frame);
        _aliPay = aliPay;
        [aliPay setTag:PAYMODE_ALIPAY];
         aliPay.delegate = self;
        [self addSubview:aliPay];
        
        //8.微信支付
        AccountCenterPayModeView *wxPay = [[AccountCenterPayModeView alloc]init];
        [wxPay setIcon:@"account_center_wxpay_icon"];
        [wxPay setDesc:@"微信支付"];
        [wxPay setCondition:@"满69元包邮"];
        [wxPay setIsChecked:NO];
        [wxPay setIsHidenLine:YES];
        wxPay.x = 0;
        wxPay.y = CGRectGetMaxY(self.aliPay.frame);
        _wxPay = wxPay;
        [wxPay setTag:PAYMODE_WXPAY];
         wxPay.delegate = self;
        [self addSubview:wxPay];
        
        //9.微信支付下划线
        UIView * wxPaylineView = [[UIView alloc]initWithFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(self.wxPay.frame), JXScreenW-JXPublicInset, 0.5)];
        [wxPaylineView setBackgroundColor:JXColor(204, 204, 204)];
        _wxPaylineView = wxPaylineView;
        [self addSubview:wxPaylineView];
        
        //10.现金券
        UIImageView *volumeTitleIconView =[[UIImageView alloc]init];
        UIImage *volumeTileIcon = [UIImage imageNamed:@"account_center_volume_icon"];
        [volumeTitleIconView setImage:volumeTileIcon];
        [volumeTitleIconView setFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(self.wxPaylineView.frame)+32, volumeTileIcon.size.width, volumeTileIcon.size.height)];
        _volumeTitleIconView = volumeTitleIconView;
        [self addSubview:volumeTitleIconView];
        
        //11.现金券UILabel
        NSString *volumeTitleLabelText = @"现金券";
        UILabel *volumeTitleLabel = [[UILabel alloc]init];
        [volumeTitleLabel setText:volumeTitleLabelText];
        [volumeTitleLabel setTextColor:titleLabelColor];
        [volumeTitleLabel setFont:[UIFont systemFontOfSize:ACCOUNT_CENTER_FOOTER_VIEW_TITLE]];
        CGSize volumeTitleLabelTextSize = [volumeTitleLabelText sizeWithFont:[UIFont systemFontOfSize:ACCOUNT_CENTER_FOOTER_VIEW_TITLE]];
        [volumeTitleLabel setFrame:CGRectMake(CGRectGetMaxX(self.volumeTitleIconView.frame)+5, CGRectGetMaxY(self.wxPaylineView.frame)+30, volumeTitleLabelTextSize.width, volumeTitleLabelTextSize.height)];
        _volumeTitleLabel = volumeTitleLabel;
        [self addSubview:volumeTitleLabel];
        
        //12.现金券下划线
        UIView *volumeLineView = [[UIView alloc]initWithFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(self.volumeTitleIconView.frame)+JXPublicInset, JXScreenW-JXPublicInset, 0.5)];
        [volumeLineView setBackgroundColor:JXColor(204, 204, 204)];
        _volumeLineView = volumeLineView;
        [self addSubview:volumeLineView];
        
        //现金券
        VolumeFooterView *volumeView = [[VolumeFooterView alloc]init];
        volumeView.x = JXPublicInset;
        volumeView.y = CGRectGetMaxY(volumeLineView.frame);
        self.volumeView = volumeView;
        volumeView.userInteractionEnabled = YES;
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickVolume)];
        [volumeView addGestureRecognizer:singleTap];
        [self addSubview:volumeView];
        
        //13.悄悄话UIImageView
        UIImageView *feedbackTitleIconView =[[UIImageView alloc]init];
        UIImage *feedbackTileIcon = [UIImage imageNamed:@"account_center_feedback_icon"];
        [feedbackTitleIconView setImage:feedbackTileIcon];
        [feedbackTitleIconView setFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(self.volumeView.frame)+JXPublicInset*2, feedbackTileIcon.size.width, feedbackTileIcon.size.height)];
        _feedbackTitleIconView = feedbackTitleIconView;
        [self addSubview:feedbackTitleIconView];
        
        //14.悄悄话UILabel
        NSString *feedbackTitleText = @"悄悄话";
        UILabel *feedbackTitleLabel = [[UILabel alloc]init];
        [feedbackTitleLabel setText:feedbackTitleText];
        [feedbackTitleLabel setTextColor:titleLabelColor];
        [feedbackTitleLabel setFont:[UIFont systemFontOfSize:ACCOUNT_CENTER_FOOTER_VIEW_TITLE]];
        CGSize feedbackTitleTextSize = [feedbackTitleText sizeWithFont:[UIFont systemFontOfSize:ACCOUNT_CENTER_FOOTER_VIEW_TITLE]];
        [feedbackTitleLabel setFrame:CGRectMake(CGRectGetMaxX(self.feedbackTitleIconView.frame)+5, CGRectGetMaxY(self.volumeView.frame)+JXPublicInset*2, feedbackTitleTextSize.width, feedbackTitleTextSize.height)];
        _feedbackTitleLabel = feedbackTitleLabel;
        [self addSubview:feedbackTitleLabel];
        
        //15.悄悄话下划线
        UIView *feedbackLineView = [[UIView alloc]initWithFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(self.feedbackTitleIconView.frame)+JXPublicInset, JXScreenW-JXPublicInset, 0.5)];
        [feedbackLineView setBackgroundColor:JXColor(204, 204, 204)];
        _feedbackLineView = feedbackLineView;
        [self addSubview:feedbackLineView];
        
        //16.备注输入框UITextField
        UITextField *feedbackInput = [[UITextField alloc]init];
        [feedbackInput setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:12]]];
        [feedbackInput setBackgroundColor:JXColor(240, 240, 240)];
        feedbackInput.frame = CGRectMake(JXPublicInset, CGRectGetMaxY(self.feedbackLineView.frame)+JXPublicInset, JXScreenW-JXPublicInset*2, 34.5);
        //设置边框
        [feedbackInput.layer setBorderColor:JXColor(240, 240, 240).CGColor];
        [feedbackInput.layer setBorderWidth:0.7];
        [feedbackInput.layer setCornerRadius:5];
        
        feedbackInput.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        feedbackInput.attributedPlaceholder =[[NSAttributedString alloc] initWithString:@"告诉我们您的特殊需求哦" attributes:@{NSForegroundColorAttributeName: JXColor(153, 153, 153)}];
        feedbackInput.clearButtonMode = UITextFieldViewModeAlways;
        
        //左边垫一个View
        CGRect leftframe = [feedbackInput frame];
        leftframe.size.width = 7.0f;
        UIView *leftview = [[UIView alloc] initWithFrame:leftframe];
        feedbackInput.leftViewMode = UITextFieldViewModeAlways;
        feedbackInput.leftView = leftview;
        
        _feedbackInput = feedbackInput;
        [self addSubview:feedbackInput];
        //设置self
        [self setFrame:CGRectMake(0, 0, JXScreenW, CGRectGetMaxY(self.feedbackInput.frame)+JXPublicInset)];
    }
    return self;
}

/**
 *  初始化数据
 *
 *  @param infos
 */
-(void)setInfos:(AddressAndCheckProdInfo *)infos{//
    _localInfo = infos;
    //1.设置包邮条件
    [self.cargoPay setCondition:[NSString stringWithFormat:@"满%@元包邮",infos.cashOnDelivery]];
    [self.aliPay setCondition:[NSString stringWithFormat:@"满%@元包邮",infos.linePayment]];
    [self.wxPay setCondition:[NSString stringWithFormat:@"满%@元包邮",infos.linePayment]];
    //2.设置邮费&&要选中的支付方式
    [self setSelectPayMode:infos.currentPayMode];
}

/**
 * 初始化邮费&&选择的支付方式
 */
-(void)setSelectPayMode:(int)selectPayMode{
    CalcFreightPriceModel *freight;
    if((selectPayMode == PAYMODE_ALIPAY ||selectPayMode == PAYMODE_WXPAY) && _localInfo.dprice.integerValue!=0){
        freight = [self calcLinePayFreight:_localInfo];
    }else{
        freight = [self calcCargoFreight:_localInfo];
    }
    //设置邮费
    [self setFreight:[freight freightString]];
    //设置支付方式
    switch (selectPayMode) {
        case PAYMODE_CARGO_PAY:
            [self.cargoPay setIsChecked:YES];
            [self.aliPay setIsChecked:NO];
            [self.wxPay setIsChecked:NO];
            break;
        case PAYMODE_ALIPAY:
            [self.cargoPay setIsChecked:NO];
            [self.aliPay setIsChecked:YES];
            [self.wxPay setIsChecked:NO];
            break;
        case PAYMODE_WXPAY:
            [self.cargoPay setIsChecked:NO];
            [self.aliPay setIsChecked:NO];
            [self.wxPay setIsChecked:YES];
            break;
        default:
            break;
    }
}

/**
 *  设置邮费
 *
 *  @param freight
 */
-(void)setFreight:(NSString *)freight{
    [self.freightRightLabel setText:freight];
}

/**
 *  计算在线支付邮费
 *
 *  @param infos
 *
 *  @return 邮费
 */
-(CalcFreightPriceModel *)calcLinePayFreight:(AddressAndCheckProdInfo *)infos{
    NSString *newfreight = @"包邮";
    CalcFreightPriceModel *freight = [[CalcFreightPriceModel alloc]init];
    if(infos.dprice.integerValue >= infos.linePayment.integerValue){
        [freight setFreightString:newfreight];
        [freight setFreightNum:0];
    }else{
        if (infos.freight.integerValue == 0) {
            [freight setFreightString:newfreight];
            [freight setFreightNum:0];
        }else{
            [freight setFreightString:[NSString stringWithFormat:@"+%@元",infos.lineFreight]];
            [freight setFreightNum:infos.lineFreight.integerValue];
        }
    }
    return freight;
}

/**
 *   计算货到付款邮费
 *
 *  @param infos
 *
 *  @return 邮费
 */
-(CalcFreightPriceModel *)calcCargoFreight:(AddressAndCheckProdInfo *)infos{
     NSString *newfreight = @"包邮";
     CalcFreightPriceModel *freight = [[CalcFreightPriceModel alloc]init];
    
        if(infos.dprice.integerValue >= infos.cashOnDelivery.integerValue){
            [freight setFreightString:newfreight];
            [freight setFreightNum:0];
        }else{
            if(infos.freight.integerValue == 0){
                [freight setFreightString:newfreight];
                [freight setFreightNum:0];
            }else{
                [freight setFreightString:[NSString stringWithFormat:@"+%@元",infos.freight]];
                [freight setFreightNum:infos.freight.integerValue];
            }
        }
    return freight;
}

-(NSString *)getUserRemark{
    return self.feedbackInput.text;
}

/**
 *  点击支付方式
 *
 *  @param tag 当前所点击的方式的tag
 */
-(void)clickPayMode:(NSInteger)tag{
    if ([self.delegate respondsToSelector:@selector(selectPayMode:)]) {
        [_delegate selectPayMode:tag];
    }
    switch (tag) {
        case PAYMODE_CARGO_PAY:
            [self.cargoPay setIsChecked:YES];
            [self.aliPay setIsChecked:NO];
            [self.wxPay setIsChecked:NO];
            [self setFreight:[self calcCargoFreight:_localInfo].freightString];
            break;
        case PAYMODE_ALIPAY:
            [self.cargoPay setIsChecked:NO];
            [self.aliPay setIsChecked:YES];
            [self.wxPay setIsChecked:NO];
            [self setFreight:[self calcLinePayFreight:_localInfo].freightString];
            break;
        case PAYMODE_WXPAY:
            [self.cargoPay setIsChecked:NO];
            [self.aliPay setIsChecked:NO];
            [self.wxPay setIsChecked:YES];
            [self setFreight:[self calcLinePayFreight:_localInfo].freightString];
            break;
        default:
            break;
    }
}

/**
 *  设置代金卷的数据
 *
 *  @param num   现金券数量
 *  @param price 现金券价格
 */
-(void)setVolumeText:(NSUInteger)num price:(NSUInteger)price{
    [self.volumeView setVolumeNum:num price:price];
//    if(num == 0){
//        [self.volumeLineView setHidden:YES];
//        [self.volumeTitleIconView setHidden:YES];
//        [self.volumeTitleLabel setHidden:YES];
//        [self.volumeView setHidden:YES];
//        [self resetVolumeLocationToHide];
//    }else{
//        [self.volumeLineView setHidden:NO];
//        [self.volumeTitleIconView setHidden:NO];
//        [self.volumeTitleLabel setHidden:NO];
//        [self.volumeView setHidden:NO];
//        [self.volumeView setVolumeNum:num price:price];
//    }
}

/**
 *  重置一下相对的y轴位置，处理空白
 */
-(void)resetVolumeLocationToHide{
    self.feedbackTitleIconView.y =CGRectGetMaxY(self.wxPaylineView.frame)+ 32;
    self.feedbackTitleLabel.y =CGRectGetMaxY(self.wxPaylineView.frame)+30;
    self.feedbackLineView.y = CGRectGetMaxY(self.feedbackTitleIconView.frame)+JXPublicInset;
    self.feedbackInput.y = CGRectGetMaxY(self.feedbackLineView.frame)+JXPublicInset;
    [self setFrame:CGRectMake(0, 0, JXScreenW, CGRectGetMaxY(self.feedbackInput.frame)+JXPublicInset)];
}

/**
 *  点击现金券
 */
-(void)clickVolume{
    [_delegate clickVolume];
}

@end
